package com.boolib.animate.framework.service.advice;

import com.boolib.animate.commons.Consts;
import com.boolib.animate.framework.entity.AnimateCollectWork;
import com.boolib.animate.framework.entity.AnimateUser;
import com.boolib.animate.framework.entity.AnimateWork;
import com.boolib.animate.framework.service.base.impl.BaseAnimateCollectWorkService;
import com.boolib.animate.framework.service.base.impl.BaseAnimateWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nier
 * @create 2020-03-02 23:55
 */
@Service
public class AnimateCollectWorkService {
    @Autowired
    private BaseAnimateWorkService baseAnimateWorkService;

    @Autowired
    private BaseAnimateCollectWorkService baseAnimateCollectWorkService;

    public List<AnimateWork> queryCollect(Long id) {
        AnimateCollectWork animateCollectWork = new AnimateCollectWork();

        animateCollectWork.setUserId(id);

        animateCollectWork.setIsDel(Consts.ENABLE);

        animateCollectWork.setShowStatus(Consts.ENABLE);

        final List<AnimateCollectWork> query = baseAnimateCollectWorkService.query(animateCollectWork);

        List<AnimateWork> list = new ArrayList<>();

        query.forEach(item->{

            Example example = new Example(AnimateWork.class);

            example.createCriteria().andEqualTo("isDel",Consts.ENABLE).andEqualTo("showStatus",Consts.ENABLE).andEqualTo("id",item.getWorkId());

            final AnimateWork animateWork = baseAnimateWorkService.queryOneByExample(example);

            if(animateWork!=null){

                list.add(animateWork);
            }

        });
        return list;
    }

    public boolean isCollect(Long userId,Long workId) {
        Example example = new Example(AnimateCollectWork.class);
        example.createCriteria().andEqualTo("isDel",Consts.ENABLE).andEqualTo("showStatus",Consts.ENABLE).andEqualTo("workId",workId).andEqualTo("userId",userId);
        return  baseAnimateCollectWorkService.queryByExample(example).size()>0;
    }

    public boolean collect(Long userId, Long workId) {

        Example example = new Example(AnimateCollectWork.class);

        example.createCriteria().andEqualTo("isDel",Consts.ENABLE).andEqualTo("showStatus",Consts.ENABLE).andEqualTo("workId",workId).andEqualTo("userId",userId);

        final AnimateCollectWork animateCollectWork = baseAnimateCollectWorkService.queryOneByExample(example);
        if(animateCollectWork == null){
            AnimateCollectWork newAnimateCollectWork = new AnimateCollectWork();
            newAnimateCollectWork.setWorkId(workId);
            newAnimateCollectWork.setUserId(userId);
            baseAnimateCollectWorkService.save(newAnimateCollectWork);
            return true;
        }else{
            baseAnimateCollectWorkService.delete(animateCollectWork);
          return false;
        }


    }
}
